﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using System.Data;

namespace Reconciliation.Data
{
    public partial class Invoice
    {
        public static Domain.Invoice MapDataToDomain(Data.Invoice dataInvoice)
        {
            Domain.Invoice domainInvoice = new Reconciliation.Domain.Invoice();
            domainInvoice.Amount = dataInvoice.Amount ;
            domainInvoice.AmountPaid = dataInvoice.AmountPaid ;            
            domainInvoice.ClientShortage = dataInvoice.ClientShortage ;
            domainInvoice.InvoiceDate = dataInvoice.InvoiceDate ;
            domainInvoice.InvoiceStatus = dataInvoice.InvoiceStatus ;
            domainInvoice.InvoiceType = dataInvoice.InvoiceType ;
            domainInvoice.MsmShortage = dataInvoice.MsmShortage ;

            return domainInvoice;
        }

        public static Data.Invoice MapDomainToData(Domain.Invoice domainInvoice)
        {
            Data.Invoice dataInvoice = new Reconciliation.Data.Invoice();
            dataInvoice.Amount = domainInvoice.Amount;
            dataInvoice.AmountPaid = domainInvoice.AmountPaid;
            dataInvoice.ClientShortage = domainInvoice.ClientShortage;
            dataInvoice.InvoiceDate = domainInvoice.InvoiceDate;
            dataInvoice.InvoiceStatus = domainInvoice.InvoiceStatus;
            dataInvoice.InvoiceType = domainInvoice.InvoiceType;
            dataInvoice.MsmShortage = domainInvoice.MsmShortage;
            dataInvoice.BankReference.EntityKey = new System.Data.EntityKey("ReconciliationEntities.Invoice", "InvoiceID", domainInvoice.Bank.BankID);
            return dataInvoice;
        }

        public static List<Domain.Invoice> GetInvoiceListMonthWise(DateTime startDate, DateTime endDate, int bankID, string invoiceType)
        {
            try
            {
                ReconciliationEntities recEnt = new ReconciliationEntities();
                List<Domain.Invoice> domainInvoiceList = (from i in recEnt.Invoice
                                                          where i.Bank.BankID.Equals(bankID) && i.InvoiceType.Equals(invoiceType) &&
                                                                i.InvoiceDate >= startDate && i.InvoiceDate <= endDate
                                                          select new Domain.Invoice
                                                          {
                                                              Amount = i.Amount,
                                                              AmountPaid = i.AmountPaid,
                                                              ClientShortage = i.ClientShortage,
                                                              InvoiceDate = i.InvoiceDate,
                                                              InvoiceID = i.InvoiceID,
                                                              InvoiceStatus = i.InvoiceStatus,
                                                              InvoiceType = i.InvoiceType,
                                                              MsmShortage = i.MsmShortage,
                                                              StatusDate = i.StatusDate,
                                                              isCommentExists = i.InvoiceAuditorComment.Count == 0 ? false : true  
                                                          }).ToList();
                return domainInvoiceList;
            }
            catch (EntityException ex)
            {
                ExceptionPolicy.HandleException(ex, "Exception Policy");
                throw new Domain.ReconciliationException("Database exception occured please try again later.");
            }
            catch (Exception ex)
            {
                ExceptionPolicy.HandleException(ex, "Exception Policy");
                throw new Domain.ReconciliationException("Un able to retrieve invoice list.");
            }
        }        
    }
}
